<template>
  <div class="holder">
    <h1>Generate Machine</h1>
    <el-button @click="generate" type="primary">Generate Mnemonic</el-button>
    <div>mnemonic: {{ mnemonic }}</div>

    <hr style="margin:40px 0" color="#987cb9" />

    <h1>Transfer Machine</h1>
    <el-input
      type="textarea"
      :autosize="{ minRows: 4, maxRows: 4 }"
      placeholder="input"
      v-model="textareaIn"
      class="textarea"
      @input="transfer"
    >
    </el-input>
    in words count: {{ inCount }}

    <h2 @click="abc">Result</h2>
    <el-radio-group v-model="transferType" size="medium" class="radio_group" @change="transfer">
      <el-radio-button label="num">Number</el-radio-button>
      <el-radio-button label="en">English</el-radio-button>
      <el-radio-button label="ch">Chinese</el-radio-button>
    </el-radio-group>
    <el-input type="textarea" :rows="4" placeholder="output" v-model="textareaOut" class="textarea"> </el-input>
    <div>out words count: {{ outCount }}</div>
    <div>is mnemonic valid? {{ isValid }}</div>

    <hr style="margin:40px 0" color="#987cb9" />

    <h1>Violence The Last Word</h1>
    <el-input type="textarea" :rows="4" placeholder="only accept English words" v-model="textareaNeedLast" class="textarea"> </el-input>
    <br><br>
    <el-button @click="generateCheckBit" type="primary">Generate The Last Word</el-button>
    <br><br>
    <p v-if="mnemonicPadded.length>0">total: {{count}}</p>
    <p v-for="(item,index) in mnemonicPadded" :key="index">
      {{item}}
    </p>

    <br><br>
    <br><br>
    <br><br>

  </div>
</template>

<script>
const bip39 = require("bip39");
export default {
  name: "App",
  components: {},
  data() {
    return {
      enArr:[],
      chArr:[],
      mnemonic: "",
      textareaIn: "",
      textareaOut: "",
      transferType: "en",
      textareaNeedLast: "",  //待
      mnemonicPadded:[],
      count:0,
      isValid: false, //默认不有效
    };
  },
  computed: {
    inCount() {
      if (this.textareaIn == "") {
        return 0;
      } else {
        return this.textareaIn.trim().split(" ").length;
      }
    },
    outCount() {
      if (this.textareaOut == "") {
        return 0;
      } else {
        return this.textareaOut.trim().split(" ").length;
      }
    },
  },
  methods: {
    generate() {
      this.mnemonic = bip39.generateMnemonic();
      // this.mnemonic = bip39.generateMnemonic(128, null, bip39.wordlists.chinese_simplified);

      const Entropy = bip39.mnemonicToEntropy("wing truth waste liquid aisle near novel survey few lady baby because");
      console.log("Entropy", Entropy);

      // this.seed=bip39.mnemonicToSeedSync(this.mnemonic).toString('hex')

      // bip39
      //   .mnemonicToSeed(this.mnemonic)
      //   .then((bytes) => bytes.toString("hex"))
      //   .then((data) => {
      //     console.log(data);
      //     this.seed=data
      //   });
    },
    isChinese(temp) {
      const pattern = new RegExp("[\u4E00-\u9FA5]+");
      return pattern.test(temp);
    },
    isNumber(temp) {
      const pattern = new RegExp("[0-9]+");
      return pattern.test(temp);
    },
    isEnglish(temp) {
      const pattern = new RegExp("[A-Za-z]+");
      return pattern.test(temp);
    },
    getLang(temp) {
      let result;
      if (this.isChinese(temp)) {
        result = "ch";
      } else if (this.isEnglish(temp)) {
        result = "en";
      } else if (this.isNumber(temp)) {
        result = "num";
      }
      return result;
    },
    //转换过程
    transfer() {
      const arr = (this.textareaIn + "").split(" ");
      if (arr.length == 0) {
        return;
      }
      const currentLang = this.getLang(arr[0][0]);
      // console.log(currentLang);
      //获得序列
      const indexArr = this.getIndexArr(arr, currentLang);

      //将序列转化成目标
      this.textareaOut = this.getDistArr(indexArr).join(" ");

      this.isValid = bip39.validateMnemonic(this.textareaOut);
    },
    //把input转成序列
    getIndexArr(arr, currentLang) {
      let resultArr = [];
      if (currentLang == "ch") {
        arr.map((item) => {
          this.chArr.map((cell, index) => {
            if (item == cell) {
              resultArr.push(index);
            }
          });
        });
      } else if (currentLang == "en") {
        arr.map((item) => {
          this.enArr.map((cell, index) => {
            if (item == cell) {
              resultArr.push(index);
            }
          });
        });
      } else if (currentLang == "num") {
        arr.map((item) => {
          resultArr.push(item * 1);
        });
      }
      return resultArr;
    },
    //把序列转化成结果
    getDistArr(indexArr) {
      let resultArr = [];
      if (this.transferType == "ch") {
        indexArr.map((item) => {
          resultArr.push(this.chArr[item]);
        });
      } else if (this.transferType == "en") {
        indexArr.map((item) => {
          resultArr.push(this.enArr[item]);
        });
      } else if (this.transferType == "num") {
        resultArr = indexArr;
      }
      return resultArr;
    },
    //已经输入了11个word，暴力算出第12个
    generateCheckBit(){
      let arr=this.textareaNeedLast.trim().split(' ')
      if(arr.length!==11){
        alert('must be 11 English words!')
        return
      }

      let tryArr=[]
      this.count=0
      this.mnemonicPadded=[]
      this.enArr.map(item=>{
        tryArr=[]
        tryArr=arr.concat(item)
        if(bip39.validateMnemonic(tryArr.join(' '))){
          this.mnemonicPadded.push(tryArr.join(' '))
          this.count+=1
        }
      })
      
    },
    

  },
  mounted() {
    this.chArr=bip39.wordlists.chinese_simplified
    this.enArr=bip39.wordlists.english
  },
};
</script>

<style scoped lang="less">
@import "./app.less";
</style>
